package com.thd.framework.db.intereptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.thd.framework.basic.obligate.CurrentUserUtil;
import com.thd.framework.basic.utils.SpringUtils;
import com.thd.framework.basic.utils.StringUtils;
import com.thd.framework.db.entity.BasicEntity;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

@Component
public class MyBatisPlusDefaultDBFieldHandler implements MetaObjectHandler {

    private Logger log = LoggerFactory.getLogger(MyBatisPlusDefaultDBFieldHandler.class);
    @Override
    public void insertFill(MetaObject metaObject) {
        // 是否初始化公共字段
        boolean dealDefaultColumn = false;

        if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BasicEntity) {

            dealDefaultColumn = true;
            BasicEntity entity = null;
            try{
                entity = (BasicEntity)metaObject.getOriginalObject();
            }catch (Exception e){
                dealDefaultColumn = false;
                log.error("fill default column error :" + e.getMessage());
            }

            Date d = new Date();
            if(null == entity.getCreateTime()) {
                entity.setCreateTime(d);
            }
            if(null == entity.getModifyTime()) {
                entity.setModifyTime(d);
            }



            // 是否在IOC容器中有实现了CurrentUserUtil接口的类
            CurrentUserUtil currentUserUtil = null;
            try{
                currentUserUtil = SpringUtils.getBean(CurrentUserUtil.class);
            }catch(Exception e){
                dealDefaultColumn = false;
            }
            if(null == currentUserUtil){
                dealDefaultColumn = false;
            }

            if(dealDefaultColumn){

                if(StringUtils.isEmpty(entity.getTenantId())){
                    entity.setTenantId(currentUserUtil.getTenantId());
                }
                if(StringUtils.isEmpty(entity.getCreateBy())) {
                    entity.setCreateBy(currentUserUtil.getCurrentUserId());
                }

                if(StringUtils.isEmpty(entity.getModifyBy())) {
                    entity.setModifyBy(currentUserUtil.getCurrentUserId());
                }


            }




        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 是否初始化公共字段
        boolean dealDefaultColumn = false;

        if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BasicEntity) {

            dealDefaultColumn = true;
            BasicEntity entity = null;
            try{
                entity = (BasicEntity)metaObject.getOriginalObject();
            }catch (Exception e){
                dealDefaultColumn = false;
                log.error("fill default column error :" + e.getMessage());
            }


            Date d = new Date();
            entity.setModifyTime(d);


            // 是否在IOC容器中有实现了CurrentUserUtil接口的类
            CurrentUserUtil currentUserUtil = null;
            try{
                currentUserUtil = SpringUtils.getBean(CurrentUserUtil.class);
            }catch(Exception e){
                dealDefaultColumn = false;
            }
            if(null == currentUserUtil){
                dealDefaultColumn = false;
            }

            if(dealDefaultColumn){
                entity.setModifyBy(currentUserUtil.getCurrentUserId());
            }
        }
    }
}